﻿//********************************************************************************//
//创建日期：<创建日期，2016-11-08>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：取水口信息维护
//********************************************************************************//

using System;
using System.Data;
using System.Web.UI;
using BLL;
using BLL.Access;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.YongHuGL
{
    public partial class QuShuiKXXWH : BasePage
    {
        #region 属性

        /// <summary>
        ///     gridView主键(用户信息ID)
        /// </summary>
        private int QSK_ID
        {
            get { return Convert.ToInt32(ViewState["_QSK_ID"]); }
            set { ViewState["_QSK_ID"] = value; }
        }

        /// <summary>
        ///     保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get { return ViewState["dtSource"] as DataTable; }
            set { ViewState["dtSource"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     用户信息BLL
        /// </summary>
        private readonly QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     深井信息
        /// </summary>
        private QS_YH_QUSHUIKXXB _QSKXX = new QS_YH_QUSHUIKXXB();

        /// <summary>
        ///     深井信息BLL
        /// </summary>
        private readonly QS_YH_QUSHUIKXXBBLL _QSKXXBLL = new QS_YH_QUSHUIKXXBBLL();

        /// <summary>
        ///     日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
            }
        }

        /// <summary>
        ///     重写Render方法
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }


        /// <summary>
        ///     放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Init;
            Display();
            //如果当前有操作项 重新加载数据
            if (QSK_ID > 0)
                SetData();
        }

        /// <summary>
        ///     修改
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtQuShuiKBH.Text == "")
            {
                PageTools.AlertMsg(this, "取水口编号为空,请输入！");
            }
            else
            {
                var dt = _QSKXXBLL.SelectQS_YH_QUSHUIKXXBsDynamic(
                    string.Format("I_JLZT=0 and S_QUSHUIKBH='{0}' ", txtQuShuiKBH.Text.Trim()), null);
                if (dt == null || dt.Rows.Count < 1)
                {
                    QSK_ID = -1;
                    PageTools.AlertMsg(this, "该取水口编号不存在,请重新输入！");
                }
                else
                {
                    QSK_ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    Status = GridViewStatus.Update;
                    Display();
                    SetData();
                }
            }
        }

        /// <summary>
        ///     保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
                SaveData();
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化控件
        /// </summary>
        private void InitControl()
        {
            txtYongHuH.Text = "";
            txtYONGHUM.Text = "";
            txtQuShuiBPH.Text = "";
            txtQuShuiDD.Text = "";
            txtTuiShuiWZ.Text = "";
            cmbXingZhengQ.Text = "";
            cmbLiuYu.Text = "";
            cmbShuiZiYFQ.Text = "";
            cmbShuiLiFP.Text = "";
            cmbHuanXianBJ.Text = "";
            cmbQuShuiLX.Text = "";
            txtQuShuiKM.Text = "";
            txtDu.Text = "";
            txtFen.Text = "";
            txtMiao.Text = "";
            txtDu2.Text = "";
            txtFen2.Text = "";
            txtMiao2.Text = "";
            txtQuShuiSY.Text = "";
            cmbQuShuiFS.Text = "";
            cmbShuiYuanSZ.Text = "";
            memoSYSZBZ.Text = "";
            txtBeiZhu.Text = "";
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            PageTools.QSLoadCombox(cmbXingZhengQ, PageTools.eLoadType.行政区QS, null, "");
            PageTools.QSLoadCombox(cmbHuanXianBJ, PageTools.eLoadType.环线标记QS, null, "");
            PageTools.QSLoadCombox(cmbLiuYu, PageTools.eLoadType.流域QS, null, "");
            PageTools.QSLoadCombox(cmbShuiZiYFQ, PageTools.eLoadType.水资源分区QS, null, "");
            PageTools.QSLoadCombox(cmbShuiLiFP, PageTools.eLoadType.水利分片QS, null, "");

            var qskh = Request.QueryString["qskh"];
            if (qskh != "" && qskh != null)
            {
                txtQuShuiKBH.Text = qskh;
                btnEdit_Click(null, null);
            }
        }


        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                    txtQuShuiKBH.ClientEnabled = true;
                    txtQuShuiKBH.Focus();
                    InitControl();
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    txtQuShuiKBH.ClientEnabled = false;
                    SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    SetText(true);
                    break;
            }
        }


        /// <summary>
        ///     设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            txtQuShuiBPH.ClientEnabled = b;
            txtQuShuiDD.ClientEnabled = b;
            cmbXingZhengQ.ClientEnabled = b;
            cmbLiuYu.ClientEnabled = b;
            cmbShuiZiYFQ.ClientEnabled = b;
            cmbShuiLiFP.ClientEnabled = b;
            cmbQuShuiFS.ClientEnabled = b;
            cmbQuShuiLX.ClientEnabled = b;
            txtTuiShuiWZ.ClientEnabled = b;
            txtDu.ClientEnabled = b;
            txtFen.ClientEnabled = b;
            txtMiao.ClientEnabled = b;
            txtDu2.ClientEnabled = b;
            txtFen2.ClientEnabled = b;
            txtMiao2.ClientEnabled = b;
            cmbShuiYuanSZ.ClientEnabled = b;
            memoSYSZBZ.ClientEnabled = b;
            cmbHuanXianBJ.ClientEnabled = b;
            txtBeiZhu.ClientEnabled = b;
        }

        /// <summary>
        ///     保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //修改操作
            operateState = OperateState.Modify;
            /*if (new SqlHelper().P_QS_INSERT_QUSHUIKOU_HIS(QSK_ID, LoginInfo.CurrentUser.UserID.ToString(), null))
            {*/
            SpHelper _spHelper = new SpHelper();
            int? resmsg = 0;
            _spHelper.P_QSYH_YZXX(1, 3, this.txtQuShuiKBH.Text, out resmsg);
            if (resmsg == 0)
            {
                _spHelper.P_QS_INSERT_QUSHUIKOU_HIS(this.txtQuShuiKBH.Text, LoginInfo.CurrentUser.UserID.ToString());
            }
            //记录ID 只是用于记录修改日志
            _QSKXX = _QSKXXBLL.SelectQS_YH_QUSHUIKXXB(QSK_ID);
            _QSKXX.S_QUSHUIKPH = txtQuShuiBPH.Text;
            _QSKXX.S_QUSHUIDZ = txtQuShuiDD.Text;
            _QSKXX.S_TUISHUIWZ = txtTuiShuiWZ.Text;
            _QSKXX.S_XINGZHENGQBH = cmbXingZhengQ.Value.ToString();
            _QSKXX.S_LIUYUBH = cmbLiuYu.Value.ToString();
            _QSKXX.S_SHUIZIYBH = cmbShuiZiYFQ.Value.ToString();
            _QSKXX.S_FENGPIANBH = cmbShuiLiFP.Value.ToString();
            _QSKXX.S_HUANGXIANBH = cmbHuanXianBJ.Value.ToString();
            _QSKXX.S_QUSHUIKM = txtQuShuiKM.Text;
            _QSKXX.S_QUSHUISY = txtQuShuiSY.Text;
            var zb = txtDu.Text + "/" + txtFen.Text + "/" + txtMiao.Text + "/" + txtDu2.Text + "/" + txtFen2.Text +
                     "/" + txtMiao2.Text;
            _QSKXX.S_ZUOBIAO = zb;
            _QSKXX.S_QUSHUIFS = cmbQuShuiFS.Text != "" ? cmbQuShuiFS.Value.ToString() : "";
            _QSKXX.S_SHUIYUANSZ = cmbShuiYuanSZ.Text != "" ? cmbShuiYuanSZ.Value.ToString() : "";
            _QSKXX.S_QUSHUILX = cmbQuShuiLX.Text != "" ? cmbQuShuiLX.Value.ToString() : "";
            _QSKXX.S_SHUIYUANSZBZ = memoSYSZBZ.Text;
            _QSKXX.S_BEIZHU = txtBeiZhu.Text;
            _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-取水口信息维护", operateState,
                _QSKXX,
                null);
            var result = _QSKXXBLL.InsertOrUpdate(_QSKXX);
            if (result)
            {
                PageTools.AlertMsg(this, "保存成功！");
                Status = GridViewStatus.Init;
                Display();
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
            /* }
             else
             {
                 PageTools.AlertMsg(this, "保存失败！");
             }*/

        }

        /// <summary>
        ///     给输入框赋值
        /// </summary>
        private void SetData()
        {
            _QSKXX = _QSKXXBLL.SelectQS_YH_QUSHUIKXXB(QSK_ID);
            var dtyh = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(
                string.Format(
                    "s_yonghuh =(select S_YONGHUH from QS_YH_YONGHUQSKGX where S_QUSHUIKH='{0}' and i_jlzt=0) and i_jlzt=0",
                    _QSKXX.S_QUSHUIKBH), null);
            txtYongHuH.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUH"].ToString() : "";
            txtYONGHUM.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUM"].ToString() : "";
            txtQuShuiBPH.Text = _QSKXX.S_QUSHUIKPH;
            txtQuShuiDD.Text = _QSKXX.S_QUSHUIDZ;
            txtTuiShuiWZ.Text = _QSKXX.S_TUISHUIWZ;
            cmbXingZhengQ.Value = _QSKXX.S_XINGZHENGQBH;
            cmbLiuYu.Value = _QSKXX.S_LIUYUBH;
            cmbShuiZiYFQ.Value = _QSKXX.S_SHUIZIYBH;
            cmbShuiLiFP.Value = _QSKXX.S_FENGPIANBH;
            cmbHuanXianBJ.Value = _QSKXX.S_HUANGXIANBH;
            txtQuShuiKM.Text = _QSKXX.S_QUSHUIKM;
            txtQuShuiSY.Text = _QSKXX.S_QUSHUISY;
            cmbQuShuiLX.Value = _QSKXX.S_QUSHUILX;
            var zb = _QSKXX.S_ZUOBIAO;
            var arr = zb.Split('/');
            if (arr.Length > 0)
            {
                txtDu.Text = arr[0];
                txtFen.Text = arr[1];
                txtMiao.Text = arr[2];
                txtDu2.Text = arr[3];
                txtFen2.Text = arr[4];
                txtMiao2.Text = arr[5];
            }
            cmbQuShuiFS.Value = _QSKXX.S_QUSHUIFS;
            cmbShuiYuanSZ.Value = _QSKXX.S_SHUIYUANSZ;
            cmbHuanXianBJ.Value = _QSKXX.S_HUANGXIANBH;
            memoSYSZBZ.Text = _QSKXX.S_SHUIYUANSZBZ;
            txtBeiZhu.Text = _QSKXX.S_BEIZHU;
        }

        /// <summary>
        ///     保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (txtQuShuiDD.Text == "")
            {
                PageTools.AlertMsg(this, "地址不能为空！");
                txtQuShuiDD.Focus();
                return false;
            }
            if (txtTuiShuiWZ.Text == "")
            {
                PageTools.AlertMsg(this, "退水位置不能为空！");
                txtTuiShuiWZ.Focus();
                return false;
            }
            if (cmbXingZhengQ.Text == "")
            {
                PageTools.AlertMsg(this, "行政区不能为空！");
                cmbXingZhengQ.Focus();
                return false;
            }
            if (cmbLiuYu.Text == "")
            {
                PageTools.AlertMsg(this, "流域不能为空！");
                cmbLiuYu.Focus();
                return false;
            }
            if (cmbShuiZiYFQ.Text == "")
            {
                PageTools.AlertMsg(this, "水资源分区不能为空！");
                cmbShuiZiYFQ.Focus();
                return false;
            }
            if (cmbShuiLiFP.Text == "")
            {
                PageTools.AlertMsg(this, "水利分片不能为空！");
                cmbShuiLiFP.Focus();
                return false;
            }
            if (cmbQuShuiLX.Text == "")
            {
                PageTools.AlertMsg(this, "取水类型不能为空！");
                cmbQuShuiLX.Focus();
                return false;
            }
            if (txtQuShuiKM.Text == "")
            {
                PageTools.AlertMsg(this, "取水口名不能为空！");
                txtQuShuiKM.Focus();
                return false;
            }
            if (txtQuShuiSY.Text == "")
            {
                PageTools.AlertMsg(this, "取水水源不能为空！");
                txtQuShuiSY.Focus();
                return false;
            }
            if (cmbQuShuiFS.Text == "")
            {
                PageTools.AlertMsg(this, "取水方式不能为空！");
                cmbQuShuiFS.Focus();
                return false;
            }
            if (cmbShuiYuanSZ.Text == "")
            {
                PageTools.AlertMsg(this, "水源水质不能为空！");
                cmbShuiYuanSZ.Focus();
                return false;
            }
            return true;
        }

        #endregion
    }
}